$(document).ready(function(){
    function windowchange(){
        $('body').css('height', document.documentElement.clientHeight + 'px');
        $('.mbody').css('height', document.documentElement.clientHeight - 50 + 'px'); //120 50
    }
    var menuwidth = $('.menu').width();
    function menuRight(){
        $('.menu').animate({
            "margin-left": "0px"
        }, 100)
        $('.mr').animate({
            "margin-right": "-"+menuwidth+"px",
            "left": menuwidth+"px",
        }, 100)
    }
    function menuLeft(){
        $('.menu').animate({
            "margin-left": "-"+menuwidth+"px",
        }, 100)
        $('.mr').animate({
            "margin-right": "0px",
            "left": "0",
        }, 100)
    }
    function init(){
        $('a').unbind("click").click(function(){
            if($(this).attr('plink') == 'false'){
                palert.open("#aAlert");
                mthis = $(this);
                $.get($(this).attr('href'), function(data, status, xhr){ 
                    try {
                        eval(data);
                    } catch (e) {
                        alert(e);
                    }
                    palert.close("#aAlert");
                });
                return false;
            }
            return true;
        })
    }
    windowchange();
    window.onresize = windowchange
    client = {'x':-1, 'y':-1}
    document.addEventListener('touchstart',function(event){//getElementById('mbody').
        client['x'] = event.touches[0].clientX
        client['y'] = event.touches[0].clientY
    },false);
    document.addEventListener('touchmove',function(event){//getElementById('mbody').
        if(client['x'] == -1 || client['y'] == -1){
            client['x'] = event.touches[0].clientX
            client['y'] = event.touches[0].clientY
        }
        x = event.touches[0].clientX - client['x']
        y = event.touches[0].clientY - client['y']
        if(x>8){
            menuRight()
            client['y'] = client['x'] = -1
        }else if(x<-8){
            menuLeft()
            client['y'] = client['x'] = -1
        }
    },false);
    $('.m-left').click(function(){
        menuRight();
    })
    $('.m-lright').click(function(){
        menuLeft();
    })
    // $('.break').click(function(){
    //     history.go(-1)
    // })
    $('.a-li-list').click(function(){
        var stmp = $(this).find('.list');

        var b = stmp.css('height')=='0px'
        $('.a-li-list').find('.list').css('height', '0px');
        $(".a-li-list").css('color', $(".a-li-list").attr('color'));
        $(".a-li-list").css('background', $(".a-li-list").attr('background'));
        $(".a-li-list").find('li').css('color',  $(".a-li-list").find('li').attr('color'));
        $(".a-li-list").find('.list').css('background', $(".a-li-list").find('.list').attr('background'));
        if(b){
            var listmp = stmp.find('li');
            var totalheight = 0;
            for(var i=0;i<listmp.length;i++){
                totalheight += $(listmp[i]).height();
            }
            stmp.css('height', totalheight + 10 + 'px');
            $(this).attr('color', $(this).css('color'));
            $(this).attr('background', $(this).css('background'));
            $(this).css('color', '#191919');
            $(this).css('background', '#EAEAEA');
            $(this).find('li').attr('color', $(this).find('.a-li-title').css('color'));
            $(this).find('.list').attr('background', $(this).find('.a-li-title').css('background'));
            $(this).find('li').css('color', '#191919!important');
            $(this).find('.list').css('background', '#fff');
        }
    });
    $('.number').keyup(function(){
        this.value=this.value.replace(/\D/g,'')
    });

    palert = {
        open : function(newalert){
            var bodyheight = $('body').height();
            var bodywidth = $('body').width();
            $(newalert).css('display', 'block');
            var palertheight = $(newalert).height();
            var palertwidth = $(newalert).width();
            var top = $('body').height()/2 - $(newalert).height()/2;
            var left = $('body').width()/2 - $(newalert).width()/2;
            $(newalert).css('top', top + "px");
            $(newalert).css('left', left + "px");
            html = "<div class='nalert' style='position: absolute;top: 0px;left: 0px;height: "+bodyheight+"px;width: "+bodywidth+"px'></div>";
            $('body').append(html);
        },
        close: function(newalert){
            $(newalert).css('display', 'none');
            $(".nalert").remove();
        },
    }

    ScrollItem = {
        bind          : function(url){
            var itemAfter     = "";
            var itemB         = true;
            var itemPage      = 0;
            var itemIndexPage = 1;
            $('.mbody').bind("scroll",function() {
                if (document.getElementById("mbody").scrollTop + $("#mbody").height() >= $(".item").height() && itemB) {
                    itemB = false;
                    if(itemAfter == ""){
                        itemAfter = "<div class='after'>" + $(".item .after").html() + "</div>";
                        itemPage = parseInt($('.item .after').attr('page'));
                    }else{
                        $(".item").append(itemAfter);
                    }
                    $(".item .after").css('display', 'block');
                    itemIndexPage += 1;

                    $.get(url + itemIndexPage, function(data, status, xhr){ 
                        $(".item .after").css('display', 'none');
                        $(".item .after").remove()

                        if(status == 'success'){
                            $(".item").append(data);
                            if(itemPage == itemIndexPage){
                                itemB = false;
                            }else{
                                itemB = true;
                            }
                            init();
                            try{start()}catch(e){};
                            
                        }
                    });

                }
            });
        },
    }
    init();
});
